package com.jxmcloud.business.cut.service.impl.app.gift;

import com.jxmcloud.business.cut.constants.CutErrorMsg;
import com.jxmcloud.business.cut.dto.client.GetGiveUpModelConfigDto;
import com.jxmcloud.business.cut.dtoCommon.GiveUpConfig;
import com.jxmcloud.business.cut.mapper.database.IUserGiveUpAwardRecordDBMapper;
import com.jxmcloud.business.cut.model.database.DBUserActivityInfo;
import com.jxmcloud.business.cut.model.database.DBUserGiveUpAwardConfig;
import com.jxmcloud.business.cut.model.database.DBUserGiveUpAwardRecord;
import com.jxmcloud.business.cut.model.database.DBUserInviteSuccess;
import com.jxmcloud.business.cut.service.database.IUserActivityInfoDBService;
import com.jxmcloud.business.cut.service.database.IUserGiveUpAwardConfigDBService;
import com.jxmcloud.business.cut.service.database.IUserGiveUpAwardRecordDBService;
import com.jxmcloud.business.cut.service.impl.database.UserInviteSuccessDBServiceImpl;
import com.jxmcloud.business.cut.service.logic.IUserGiveUpHandle;
import com.jxmcloud.business.cut.util.SerialNumberUtil;
import com.jxmcloud.business.cut.vo.client.GetGiveUpModelConfigVo;
import com.jxmcloud.business.cut.vo.toDb.ToDbUserGiveUpAwardRecord;
import com.xlcloud.platform.api.exception.BusinessException;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.service.app.gift.IGiveUpService;
import com.jxmcloud.business.cut.vo.client.GiveUpModelVo;
import com.jxmcloud.business.cut.dto.client.GiveUpModelDto;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class GiveUpServiceImpl implements IGiveUpService {
    @Autowired
    private IUserActivityInfoDBService userActivityInfoDBService;
    @Autowired
    private UserInviteSuccessDBServiceImpl userInviteSuccessDBService;
    @Autowired
    private IUserGiveUpAwardRecordDBService userGiveUpAwardRecordDBService;
    @Autowired
    private IUserGiveUpAwardConfigDBService userGiveUpAwardConfigDBService;
    @Autowired
    private IUserGiveUpHandle userGiveUpHandle;

    @Override
    public GetGiveUpModelConfigVo getGiveUpModelConfig(GetGiveUpModelConfigDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_FALSE;
        GetGiveUpModelConfigVo vo = new GetGiveUpModelConfigVo();
        do {
            List<DBUserGiveUpAwardConfig> userGiveUpAwardConfigs = userGiveUpAwardConfigDBService.getBaseMapper().selectList(null);
            if(userGiveUpAwardConfigs.isEmpty()){
                error = "当前配置奖励信息为空";
            }
            // 1. 获取用户实际助理人数
            List<DBUserInviteSuccess> inviteSuccesses = userInviteSuccessDBService.selectAll(player.getBase().getUid(),player.getActivity().getActivityId());

            // 2. 获取配置信息
            List<GiveUpConfig> giveUpModelConfigs = userGiveUpAwardConfigDBService.getGiveUpModelConfig();

            vo.setList(giveUpModelConfigs);
            vo.setInviteSum(inviteSuccesses.size());
            error = CutErrorMsg.ERROR_TRUE;
        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return vo;
    }

    // 放弃模式
    @Override
    public GiveUpModelVo giveUpModel(GiveUpModelDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_FALSE;
        GiveUpModelVo giveUpConfig = new GiveUpModelVo();
        do {
            // 1. 检查用户是否在活动中
            DBUserActivityInfo dbUserActivityInfo = userActivityInfoDBService.selectByUidAndActivityIdOnDoing(player.getBase().getUid());
            if (dbUserActivityInfo == null) {
                error = "用户不在活动中";
                break;
            }

            // 2. 验证用户能获取哪个放弃模式奖励
            DBUserGiveUpAwardConfig dbUserGiveUpAwardConfig = userGiveUpHandle.checkUserGiveUpAward(player);
            if (dbUserGiveUpAwardConfig == null) {
                error = "用户不能获取任何奖励";
                break;
            }

            // 2. 封装奖励信息数据
            ToDbUserGiveUpAwardRecord toDbUserGiveUpAwardRecord = getToDbUserGiveUpAwardRecord(player, dbUserActivityInfo, dbUserGiveUpAwardConfig);

            // 3. 插入用户放弃模式领取奖励记录
            userGiveUpAwardRecordDBService.insertIntoData(toDbUserGiveUpAwardRecord);

            // 4. 终止用户当前活动
            dbUserActivityInfo.setStatus(-1);
            userActivityInfoDBService.updateData(dbUserActivityInfo);

            giveUpConfig.setGainId(dbUserGiveUpAwardConfig.getId());
            error = CutErrorMsg.ERROR_TRUE;
        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return giveUpConfig;
    }

    /**
     * 组装用户放弃模式领取奖励记录
     * @param player
     * @param dbUserActivityInfo
     * @param dbUserGiveUpAwardConfig
     * @return
     */
    private ToDbUserGiveUpAwardRecord getToDbUserGiveUpAwardRecord(PlayerInfoVo player, DBUserActivityInfo dbUserActivityInfo, DBUserGiveUpAwardConfig dbUserGiveUpAwardConfig) {
        ToDbUserGiveUpAwardRecord toDbUserGiveUpAwardRecord = new ToDbUserGiveUpAwardRecord();
        toDbUserGiveUpAwardRecord.setUid(player.getBase().getUid());
        toDbUserGiveUpAwardRecord.setActivityId(player.getActivity().getActivityId());
        toDbUserGiveUpAwardRecord.setUserActivityInfoId(dbUserActivityInfo.getId().intValue());
        toDbUserGiveUpAwardRecord.setGoodsName(dbUserGiveUpAwardConfig.getGoodsName());
        toDbUserGiveUpAwardRecord.setGoodsCount(dbUserGiveUpAwardConfig.getGoodsCount());
        return toDbUserGiveUpAwardRecord;
    }

}
